$(document).ready(
		function() {
			showProgresstUI();
			$("#btn-list").click(function() {
				showTextUI();
			});
			$("#btn-graph").click(function() {
				showGraphUI();
			});
			$("#statics").click(function() {

			});
			$(".titlefont").click(function() {

			})

			var keyword = getQueryString("keyword");
			$.ajax({
				"type" : "get",
				// "contentType": "application/x-www-form-urlencoded;
				// charset=utf-8",

				"url" : "../../HotInfoSupervisionServlet?method=newsInfo",
				// "timeout":10000,
				"data" : {
					"wd" : keyword
				},
				success : function(data) {
					// data = JSON.stringify(data);
					// var total = JSON.parse(data).total;
					 console.log("data:"+data);
					var newsdata = JSON.parse(data).newslist;
					console.log("newsdata:"+newsdata); 
					for (var i = 0; i < newsdata.length; i++) {
						console.log(newsdata[i]);
						var url = newsdata[i].url;
						//舆情分析的总值
						var stm = sentimentStatistics(newsdata[i].sentiment);
						console.log(stm);
						var tr = "<tr class='sentimentListStyle'><td>";
						if(stm != null){
							tr += stm + "</td><td>";
						}
						tr += "<a taget='_blank' href='" + url + "'>"
								+ newsdata[i].title + "</a></td>" + "<td>"
								+ newsdata[i].publisher + "</td><td>"
								+ newsdata[i].reprint + "</td><td>";
						console.log(tr);
						var keywordsData = newsdata[i].keywords;
						if (keywordsData != null) {
							// 只取前三个权值最大的关键词
							if (keywordsData.length >= 3)
								for (var k = 0; k < 3; k++) {
									tr += keywordsData[k].word + "("
											+ keywordsData[k].weight + ")";
									console.log(tr);
								}
							else {
								for (var k = 0; k < keywordData.length; k++) {
									tr += keywordsData[k].word + "("
											+ keywordsData[k].weight + ")";
								}
							}
						} else {
							tr += "算法处理出错"
						}
						tr += "</td></tr>";
						$("#sentimentListTable").append(tr);
					}
					 var media_ratio = JSON.parse(data).media_ratio;
					 console.log("media_ratio="+media_ratio);
					 chart1(media_ratio);
					 var reprintCommentRatio = JSON.parse(data).reprint_comment_ratio;
					 console.log("reprintCommentRatio="+reprintCommentRatio);
					 chart2(reprintCommentRatio);
					showResultUI();
				},
				error : function() {
					console.log("error");
				},

			/*
			 * complete:function(XMLHttpRequest,status){ if(status=="timeout"){ //
			 * ajaxTimeOut.abort(); //取消请求 alert("超时"); // phantom.exit(); } }
			 */

			});
		})
function sentimentStatistics(sentimentData){
	if(sentimentData == null)
		return null;
	var sum = 0.0
	for(var i = 0 ; i < sentimentData.length; i++){
		//data1：非负面概率  data2：负面概率
		sum += sentimentData[i].data1;
	}
	sum /= sentimentData.length;
	return sum;
}
function showResultUI() {
	$("#result").show();
	showTextUI();
	$("#progress").hide();
}
function showProgresstUI() {
	$("#result").hide();
	$("#progress").show();
}
function showGraphUI() {
	$("#text").hide();
	$("#graph").show();
}
function showTextUI() {
	$("#text").show();
	$("#graph").hide();
}
function getQueryString(paraname) {
	var reg = new RegExp("(^|&)" + paraname + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return (r[2]);
	}
	return null;
}

function chart1(ratio) {

	var chart = {
		plotBackgroundColor : null,
		plotBorderWidth : null,
		plotShadow : false
	};
	var title = {
		text : '来源占比统计'
	};
	var tooltip = {
		pointFormat : '{series.name}: <b>{point.percentage:.1f}%</b>'
	};
	var credits = {
		enabled : false
	};
	var plotOptions = {
		pie : {
			allowPointSelect : true,
			cursor : 'pointer',
			dataLabels : {
				enabled : true,
				format : '<b>{point.name}</b>: {point.percentage:.1f} %',
				style : {
					color : (Highcharts.theme && Highcharts.theme.contrastTextColor)
							|| 'black'
				}
			}
		}
	};

	/*
	 * var getRatioData = function(ratio){ var ret = "["; if(ratio == null){
	 * return ret + "]"; }
	 * 
	 * for(var i = 0 ; i < ratio.length-1 ; i++){ ret += "['" + ratio[i].source +
	 * "',"; ret += Math.round(ratio[i].ratio*100) + "],"; } ret += "['" +
	 * ratio[ratio.length-1].source + "',"; ret +=
	 * Math.round(ratio[ratio.length-1].ratio*100) + "]"; return ret + "]"; }
	 */

	var media_arr = new Array();
	for (var i = 0; i < ratio.length; i++) {
		media_arr[i] = {};
		media_arr[i].name = ratio[i].source;
		media_arr[i].y = ratio[i].ratio;
	}
	console.log("getRatioData=" + media_arr);
	var series = [ {
		type : 'pie',
		name : 'null',
		data : media_arr
	} ];

	var json = {};
	json.chart = chart;
	json.title = title;
	json.tooltip = tooltip;
	json.series = series;
	json.plotOptions = plotOptions;
	json.credits = credits;
	$('#mediaRatio').highcharts(json);

}

function chart2(data) {
	var categories = new Array();
	var commentarr = new Array();
	var reprintarr = new Array();
	
	for (var i = 0; i < data.length; i++) {
		var publisher = data[i].publisher;
		categories.push(publisher);
		var comment = data[i].comment;
		commentarr.push(comment);
		var reprint = data[i].reprint;
		reprintarr.push(reprint);
	}
	console.log("categories=" + categories);
	console.log("commentarr=" + commentarr);
	console.log("reprint = " + reprint);
	
	var credits = {
		enabled : false
	};
	var chart = {
		type : 'column'
	};
	var title = {
		text : '评论量/转载量统计'
	};
	var xAxis = {
		// categories: ['新浪微博','豆瓣','人民网','搜狐','雅虎','网易','凤凰网'],
		categories : categories,
		crosshair : true
	};
	var yAxis = {
		min : 0,
		title : {
			text : '量 (条)'
		}
	};
	var tooltip = {
		headerFormat : '<span style="font-size:10px">{point.key}</span><table>',
		pointFormat : '<tr><td style="color:{series.color};padding:0">{series.name}: </td>'
				+ '<td style="padding:0"><b>{point.y:.1f} 条</b></td></tr>',
		footerFormat : '</table>',
		shared : true,
		useHTML : true
	};
	var plotOptions = {
		column : {
			pointPadding : 0.2,
			borderWidth : 0
		}
	};

	/*
	 * var series= [{ name: '评论量', data: [65,59,90,81,56,55,40] }, { name:
	 * '转发量', data: [28,40, 40, 19, 96,27,100] }];
	 */

	var series = [ {
		name : '评论量',
		data : commentarr
	},{
		name : '转载量',
		data : reprintarr
	} ];

	var json = {};
	json.chart = chart;
	json.title = title;
	json.tooltip = tooltip;
	json.xAxis = xAxis;
	json.yAxis = yAxis;
	json.series = series;
	json.plotOptions = plotOptions;
	json.credits = credits;
	$('#reprintRatio').highcharts(json);
}